<?php

namespace App\Robot\Job;

use App\CustHub\Model\ChRoomPushMsg;
use App\Robot\Dto\RobotMsgDto;
use Hyperf\AsyncQueue\Job;
use Monolog\Logger;

class SendRobotMsgDelayedJob extends Job
{
    public RobotMsgDto $msgData;

    /**
     * 任务执行失败后的重试次数，即最大执行次数为 $maxAttempts+1 次
     */
    protected int $maxAttempts = 2;


    public function __construct(RobotMsgDto $msgData)
    {
        $this->msgData = $msgData;
    }

    public function handle(): void
    {
        try {
            $result = make(\App\Robot\Service\RobotMessageService::class, [$this->msgData])->sendMessage();

            if($this->msgData->isNeedChangeStatus() && !empty($this->msgData->getExtra())){
                $extra = $this->msgData->getExtra();
                if($extra['type'] == 0){
                    ChRoomPushMsg::query()
                        ->where('id', $extra['id'])
                        ->update(['status' => 1]);
                }
            }
        }catch (\Throwable $e){
            stdout_log()->error('发送'.$e->getMessage());
        }
    }
}